package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.HuoDongItemBean;
import com.beiing.tianshuai.tianshuai.util.DateUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 活动列表也适配器
 *
 * @author magikliu
 * @date 2017/7/14
 */

public class EventsRvAdapter extends RecyclerView.Adapter<EventsRvAdapter.ViewHolder> implements View.OnClickListener {

    private List<HuoDongItemBean.DataBean.ActiveBean> mActiveBeans;
    private Context context;
    private OnItemClickListener onItemClickListener;
    private OnItemViewClickListener onItemViewClickListener;

    public EventsRvAdapter(List<HuoDongItemBean.DataBean.ActiveBean> activeBeans, Context context) {
        this.mActiveBeans = activeBeans;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_huodong, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.huodongTitle.setText(mActiveBeans.get(position).getStrech());
        holder.huodongTime.setText(DateUtil.timeStamp2Date(mActiveBeans.get(position).getActionTime(), "yyyy-MM-dd HH:mm"));
        if (TextUtils.isEmpty(mActiveBeans.get(position).getLocation())) {
            holder.huodongSchool.setVisibility(View.INVISIBLE);
        } else {
            holder.huodongSchool.setVisibility(View.VISIBLE);
            holder.huodongSchool.setText(mActiveBeans.get(position).getLocation());
        }
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(800, 450);
        Glide.with(context.getApplicationContext()).load(HttpRequestConstant.ACTIVITY_PIC_HEAD +
                mActiveBeans.get(position).getCover())
                .apply(options)
                .into(holder.huodongImg);
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(this);
        holder.huodongPrice.setTag(position);
        String much = mActiveBeans.get(position).getMuch();
        holder.huodongPrice.setText(Double.parseDouble(much) > 0 ? "￥" + much : "免费");
        holder.huodongPrice.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return mActiveBeans == null ? 0 : mActiveBeans.size();
    }

    @Override
    public void onClick(View view) {
        if (onItemViewClickListener != null) {
            onItemViewClickListener.onItemViewClick(view, (Integer) view.getTag());
        }
        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(view, (Integer) view.getTag());
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        QMUIRadiusImageView huodongImg;
        @BindView(R.id.tv_title)
        TextView huodongTitle;
        @BindView(R.id.tv_address)
        TextView huodongSchool;
        @BindView(R.id.tv_time)
        TextView huodongTime;
        @BindView(R.id.huodong_price)
        TextView huodongPrice;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 条目点击事件监听
     */
    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    /**
     * 条目子view点击事件监听
     */
    public interface OnItemViewClickListener {
        void onItemViewClick(View v, int position);
    }

    public void setOnItemViewClickListener(OnItemViewClickListener listener) {
        this.onItemViewClickListener = listener;
    }
}